﻿package smooth.core.object.youTubeComponent {
	import app.core.action.RotatableScalable;
	import com.google.maps.LatLng;
	import flash.display.Sprite;
	import flash.geom.Point;
	import smooth.core.object.googleMapComponent.TappableMap;
	import smooth.core.object.interactiveLocalizationComponent.InteractiveLocalizationComponent;
	import smooth.core.object.localizationComponent.LocalizationComponent;
	//import smooth.core.object.flickrComponent.FlickrLoader;
	import smooth.core.object.interactiveTextComponent.InteractiveTextComponent;
	import caurina.transitions.Tweener;
	import flash.events.*;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;
	import flash.text.TextFormat;
	import flash.text.AntiAliasType;
	
	public class YouTubeComponent extends RotatableScalable {

		public static const NB_VIDEOS_RESULT:int = 10;
		
		[Embed(source="Arial.ttf", fontFamily="myFont")] 
		public var myFont:Class;
		// START - 20081121 - LSA
		public static const DISPLAY_COMPONENT:int = 1;
		public static const DISPLAY_MAP_TOOL:int = 2;
		private var displayStatus:int = DISPLAY_COMPONENT;
		// END - 20081121 - LSA
		private var youTubeObject1:YouTubeLoader;
		
		public function YouTubeComponent():void {
			// Init des visuels du composant			
			initGraphics();
			// Init des events du composant
			initEvents();			
			// Init des process du composant
			initProcess();
		}

		private function initGraphics():void {
			// Formatage du nom du composant
			var formatLabel:TextFormat= new TextFormat();
			formatLabel.font= "myFont";
			formatLabel.color= 0xFFFFFF;
			formatLabel.size= 15;
			// Nom du composant
			var tLabel: TextField;
			tLabel = new TextField();
			tLabel.embedFonts = true;
			tLabel.selectable  = false;		 
			tLabel.text = "You Tube";
			tLabel.autoSize = TextFieldAutoSize.CENTER;
			tLabel.name = "tLabel";
			tLabel.x = 0;
			tLabel.y = 0;
			tLabel.setTextFormat(formatLabel);
			// Formatage du libellé de recherche
			var formatSearchLabel:TextFormat= new TextFormat();
			formatSearchLabel.font= "myFont";
			formatSearchLabel.color= 0xFFFFFF;
			formatSearchLabel.size= 15;
			// Nom du composant
			var tSearch: TextField;
			tSearch = new TextField();
			tSearch.embedFonts = true;
			tSearch.selectable  = false;		 
			tSearch.text = "";
			tSearch.autoSize = TextFieldAutoSize.CENTER;
			tSearch.name = "tSearch";
			tSearch.x = 0;
			tSearch.y = 50;
			tSearch.visible = false;
			tSearch.setTextFormat(formatSearchLabel);
			// START - 20081121 - LSA
			// Map search cross
			var cross:Sprite = new Sprite();
			cross.name = "cross";
			cross.graphics.lineStyle(1.0, 0xFFFFFF, 1.0);
			cross.graphics.moveTo (34, 0);
			cross.graphics.lineTo(34, 68);
			cross.graphics.moveTo (0, 34);
			cross.graphics.lineTo(68, 34);
			cross.x = -34;
			cross.y = -34;
			cross.visible = false;
			// END - 20081121 - LSA
			// Construction visuelle du composant
			this.graphics.lineStyle(1.0,0xFFFFFF,1.0);
			this.graphics.beginFill(0xFFFFFF,0.75);
			this.graphics.drawCircle(0, 0, 34);
			this.graphics.beginFill(0x0F0F0F,0.75);
			this.graphics.drawCircle(0, 0, 47);
			this.graphics.endFill();
			this.addChild(tLabel);
			this.addChild(tSearch);
			// START - 20081121 - LSA
			this.addChild(cross);
			// END - 20081121 - LSA
		}
		
		private function initProcess():void {
			// Link du composant YouTube à la classe métier
			youTubeObject1 = new YouTubeLoader(this,NB_VIDEOS_RESULT);
		}
		
		private function initEvents():void {
			// START - 20081119 - LSA
			this.addEventListener(TouchEvent.MOUSE_MOVE, this.changeDisplay);
			////this.addEventListener(TouchEvent.MOUSE_DOWN, this.googleMapCanvasGeoSearchTool);
			this.addEventListener(TouchEvent.LONG_PRESS, this.googleMapCanvasGeoSearchTool, false, 0, true);
			// END - 20081119 - LSA
		}
		
		public function textSearchService(searchText:Object):void {
			if (searchText is InteractiveTextComponent) {
				var search:String = searchText.getText();
				if (search != "") {
					// Initialisation et affichage de tSearch
					this.doInitSearchLabel(search);
					// Lancement de la recherche
					youTubeObject1.searchText(search);
				}
			}
		}
		
		// START - 20081121 - LSA
		public function geoSearchService(searchGeoLocal:Object, distance:Number):void {
			if (searchGeoLocal is LocalizationComponent) {
				var lat:Number = searchGeoLocal.getLat();
				var lng:Number = searchGeoLocal.getLng();
				if (lat is Number && lng is Number && distance is Number) {
					// Initialisation et affichage de tSearch
					this.doInitSearchLabel("Lat:"+lat.toFixed(3)+" / Lng:"+lng.toFixed(3));
					// Lancement de la recherche
					youTubeObject1.searchGeoLocal(lat, lng, distance);
				}
			} else if (searchGeoLocal is InteractiveLocalizationComponent) {
				var lat2:Number = searchGeoLocal.getLat();
				var lng2:Number = searchGeoLocal.getLng();
				if (lat2 is Number && lng2 is Number && distance is Number) {
					// Initialisation et affichage de tSearch
					this.doInitSearchLabel("Lat:"+lat2.toFixed(3)+" / Lng:"+lng2.toFixed(3));
					// Lancement de la recherche
					youTubeObject1.searchGeoLocal(lat2, lng2, distance);
				}
			}			
		}
		// END - 20081121 - LSA
		
		public function doInitSearchLabel(searchLabel:String):void {
			var formatSearchLabel:TextFormat= new TextFormat();
			formatSearchLabel.font= "myFont";
			formatSearchLabel.color= 0xFFFFFF;
			formatSearchLabel.size = 15;
			var tSearch:TextField = this.getChildByName("tSearch") as TextField
			tSearch.text = searchLabel;
			tSearch.visible = true;
			tSearch.setTextFormat(formatSearchLabel);
		}
		
		public function doAnimationFadeOut():void {
			Tweener.addTween(this, { alpha:0.2, time:2, transition:"linear" } );	
		}
		
		public function doAnimationFadeIn():void {
			this.alpha = 0;
			// START - 20081121 - LSA
			//Tweener.addTween(this, { alpha:1, time:2, transition:"linear" } );	
			var alphaTarget:Number;
			if (this.displayStatus == DISPLAY_MAP_TOOL)
				alphaTarget = 0.5;
			else 
				alphaTarget = 1;
			Tweener.addTween(this, { alpha:alphaTarget, time:2, transition:"linear" } );
			// END - 20081121 - LSA
		}
		
		// START - 20081121 - LSA
		private function changeDisplay(e:TouchEvent):void {
			if (this.displayStatus == DISPLAY_COMPONENT) {
				var targetObjects:Array = this.stage.getObjectsUnderPoint(new Point(e.stageX, e.stageY));
				for (var i:int = 0; i < targetObjects.length;++i) {
					var targetObject:Object = targetObjects[i];
					if (targetObject is TappableMap) {
						this.doDisplayAsMapTool();
						this.displayStatus = DISPLAY_MAP_TOOL;
						return;
					}
				}
			} else if (this.displayStatus == DISPLAY_MAP_TOOL) {
				var mapNotFound:Boolean = true
				var targetObjects2:Array = this.stage.getObjectsUnderPoint(new Point(e.stageX, e.stageY));
				for (var j:int = 0; j < targetObjects2.length;++j) {
					var targetObject2:Object = targetObjects2[j];
					if (targetObject2 is TappableMap) {
						mapNotFound = false;
					}
				}
				if (mapNotFound) {
					this.doDisplayAsComponent();
					this.displayStatus = DISPLAY_COMPONENT;
					return;
				}
			}
		}
		
		private function doDisplayAsMapTool():void {
			var cross:Sprite = this.getChildByName("cross") as Sprite
			cross.visible = true;
			var tLabel:TextField = this.getChildByName("tLabel") as TextField
			tLabel.alpha = 0.1;
			this.alpha = 0.5;
		}
		
		private function doDisplayAsComponent():void {
			var cross:Sprite = this.getChildByName("cross") as Sprite
			cross.visible = false;
			var tLabel:TextField = this.getChildByName("tLabel") as TextField
			tLabel.alpha = 1;
			this.alpha = 1;
		}
		
		private function googleMapCanvasGeoSearchTool(e:TouchEvent):void {
			var selectedPoint:Point = new Point(0, 0);
			var selectedPointGlobal:Point = this.localToGlobal(selectedPoint);
			var targetObjects:Array = this.stage.getObjectsUnderPoint(selectedPointGlobal);
			for (var i:int = 0; i < targetObjects.length;++i) {
				var targetObject:Object = targetObjects[i];
				if (targetObject is TappableMap) {
					// Compute Lat/lng for center of component
					var selectedPointMap:Point = targetObject.globalToLocal(selectedPointGlobal);
					var selectedPointLatLng:LatLng = targetObject.fromViewportToLatLng(selectedPointMap);
					var selectedPointLat:Number = selectedPointLatLng.lat();
					var selectedPointLng:Number = selectedPointLatLng.lng();
					var label:String = "Lat:" + selectedPointLat + " / Long:" + selectedPointLng;
					var localisationObject:LocalizationComponent = new LocalizationComponent(selectedPointGlobal, selectedPointLat, selectedPointLng, label);
					selectedPointLat = selectedPointLatLng.latRadians();
					selectedPointLng = selectedPointLatLng.lngRadians();
					// Compute radius of component in km related to map
					var radiusPoint:Point = new Point(0, 34*this.scaleY);
					var radiusPointGlobal:Point = this.localToGlobal(radiusPoint);
					var radiusPointMap:Point = targetObject.globalToLocal(radiusPointGlobal);
					var radiusPointLatLng:LatLng = targetObject.fromViewportToLatLng(radiusPointMap);
					var radiusPointLat:Number = radiusPointLatLng.latRadians();
					var radiusPointLng:Number = radiusPointLatLng.lngRadians();
					var r:Number = 6371; // km
					var dLat:Number = (radiusPointLat-selectedPointLat);
					var dLon:Number = (radiusPointLng-selectedPointLng); 
					var a:Number = Math.sin(dLat/2) * Math.sin(dLat/2) +
									Math.cos(selectedPointLat) * Math.cos(radiusPointLat) * 
									Math.sin(dLon/2) * Math.sin(dLon/2); 
					var c:Number = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a)); 
					var d:int = r * c;
					if (d < 1) d = 1;
					// Call geoSearchService
					this.geoSearchService(localisationObject, d);
					return;
				}
			}
		}
		// END - 20081121 - LSA
		
	}
}